package com.ark.cloud.javacallnative.modules.storage.service.impl;

import com.ark.cloud.javacallnative.entity.Beam;
import com.ark.cloud.javacallnative.entity.SensorData;
import com.ark.cloud.javacallnative.entity.SensorDataBeam;
import com.ark.cloud.javacallnative.entity.SensorObj;

import com.ark.cloud.javacallnative.modules.storage.mapper.BeamMapper;

import com.ark.cloud.javacallnative.modules.storage.mapper.SensorDataBeamMapper;
import com.ark.cloud.javacallnative.modules.storage.mapper.SensorDataMapper;

import com.ark.cloud.javacallnative.modules.storage.mapper.SensorObjMapper;
import com.ark.cloud.javacallnative.modules.storage.service.SensorDataService;
import com.ark.cloud.javacallnative.util.UUIDUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**传感器数据
 * @author fukun
 * @create 2021/8/31  上午10:49
 * @description
 */
@Service
public class SensorDataServiceImpl extends ServiceImpl<SensorDataMapper,SensorData> implements SensorDataService {

    @Autowired
    SensorDataMapper sensorDataDao;
    @Autowired
    BeamMapper beamDao;
    @Autowired
    SensorDataBeamMapper sensorDataBeamDao;


    @Override
    public void saveSensorDataInfo(SensorData sensorData) {

        //设置主键
        sensorData.setSensorDataId(UUIDUtil.next());

        System.out.println("sensorData:"+sensorData);

        sensorDataDao.saveSensorDataInfo(sensorData);

        Long sensorDataId = sensorData.getSensorDataId();

        // beam、sensor_data_beam表插入
        ArrayList<Beam> beams = sensorData.getBeams();

        for (Beam beam : beams) {
            beam.setId(UUIDUtil.next());
            beamDao.insert(beam);
            Long beamId = beam.getId();

//            添加到中间表
            SensorDataBeam sensorDataBeam = new SensorDataBeam();

            sensorDataBeam.setId(UUIDUtil.next());

            sensorDataBeam.setBeamId(beamId);
            sensorDataBeam.setSensorDataId(sensorDataId);

            sensorDataBeamDao.insert(sensorDataBeam);

        }

    }
//
//    @Override
//    public void saveSensorDataInfo(SensorData sensorData) {
//
//        //设置主键
//        sensorData.setId(UUIDUtil.next());
//        System.out.println("sensorData:"+sensorData);
//
//        // sensor_obj表插入
//        SensorObj sensorObj = sensorData.getSensorObj();
//
////        设置主键
//        sensorObj.setSensorObjId(UUIDUtil.next());
//
//        sensorObjDao.saveSensorObjInfo(sensorObj);
//
//        // sensor_data表插入
//        sensorData.setSensorObj_Id(sensorObj.getSensorObjId());
//
//        sensorDataDao.insert(sensorData);
//
//
//        Long sensorDataId = sensorData.getId();
//
//        // beam、sensor_data_beam表插入
//        ArrayList<Beam> beams = sensorData.getBeams();
//
//        for (Beam beam : beams) {
//            beam.setId(UUIDUtil.next());
//            beamDao.insert(beam);
//            Long beamId = beam.getId();
//            SensorDataBeam sensorDataBeam = new SensorDataBeam();
//
//            sensorDataBeam.setId(UUIDUtil.next());
//
//            sensorDataBeam.setBeamId(beamId);
//            sensorDataBeam.setSensorDataId(sensorDataId);
//
//            sensorDataBeamDao.insert(sensorDataBeam);
//
//        }
//
//    }


}
